package com.lujianfei.powersearch.ui.dialog

import android.content.Context
import android.graphics.Rect
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lujianfei.powersearch.R
import com.lujianfei.powersearch.base.BaseDialogFragment
import com.lujianfei.powersearch.utils.DensityUtils
import com.lujianfei.powersearch.utils.ResUtils

class BottomDialog: BaseDialogFragment() {

    companion object {
        private const val TOP_ITEM = 0
        private const val MIDDLE_ITEM = 1
        private const val LAST_ITEM = 2
        private val ITEM_HEIGHT = DensityUtils.dip2px(51f)
        private val ITEM_TEXT_SIZE = 16f
    }

    private var recyclerview:RecyclerView ?= null
    private var mItemData = arrayListOf<ItemData>()
    private val mAdapter by lazy {  MyAdapter() }

    fun addItem(itemData:ItemData) {
        mItemData.add(itemData)
    }

    override fun showNow(manager: FragmentManager, tag: String?) {
        super.showNow(manager, tag)
        mAdapter.notifyDataSetChanged()
    }

    override fun resourceId() = R.layout.bottom_dialog

    override fun initView() {
        recyclerview = view?.findViewById(R.id.recyclerview)
        recyclerview?.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(MyItemDecoration())
            adapter = mAdapter
        }
    }

    override fun initEvent() {
    }

    override fun initData() {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_FRAME, R.style.style_dialog)
    }

    override fun onStart() {
        super.onStart()
        dialog?.setCanceledOnTouchOutside(true)
        val attributes = dialog?.window?.attributes
        attributes?.apply {
            gravity = Gravity.BOTTOM
            width = WindowManager.LayoutParams.MATCH_PARENT
        }
        dialog?.window?.attributes = attributes
        dialog?.window?.setWindowAnimations(R.style.bottom_dialog_anim)
    }

    data class ItemData( var label:String, var onClick:((BottomDialog)->Unit))

    inner class MyAdapter:RecyclerView.Adapter<RecyclerView.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return when(viewType) {
                TOP_ITEM -> {
                    MyViewHolder(genTopTextView(parent.context))
                }
                MIDDLE_ITEM -> {
                    MyViewHolder(genMiddleTextView(parent.context))
                }
                else-> {
                    MyViewHolder(genLastTextView(parent.context))
                }
            }
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (holder is MyViewHolder) {
                holder.bind(mItemData[position])
            }
        }

        override fun getItemViewType(position: Int): Int {
            return when (position) {
                0 -> {
                    TOP_ITEM
                }
                mItemData.size -1 -> {
                    LAST_ITEM
                }
                else -> {
                    MIDDLE_ITEM
                }
            }
        }

        override fun getItemCount() = mItemData.size
    }

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(itemData: ItemData) {
            if (itemView is TextView) {
                (itemView as TextView).text = itemData.label
                (itemView as TextView).setOnClickListener {
                    itemData.onClick.invoke(this@BottomDialog)
                }
            }
        }
    }

    private fun getCommonTextView(context: Context): TextView {
        val textView = TextView(context)
        textView.layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, ITEM_HEIGHT)
        textView.gravity = Gravity.CENTER
        textView.textSize = ITEM_TEXT_SIZE
        return textView
    }

    fun genTopTextView(context:Context):TextView {
        val textView = getCommonTextView(context)
        textView.setBackgroundResource(R.drawable.shape_dialog_item_top)
        textView.setTextColor(ResUtils.getColor(R.color.color_message_text))
        return textView
    }

    fun genMiddleTextView(context:Context):TextView {
        val textView = getCommonTextView(context)
        textView.setBackgroundResource(R.drawable.shape_dialog_item_middle)
        textView.setTextColor(ResUtils.getColor(R.color.color_message_text))
        return textView
    }
    fun genLastTextView(context:Context):TextView {
        val textView = getCommonTextView(context)
        textView.setBackgroundResource(R.drawable.shape_dialog_item_bottom)
        textView.setTextColor(ResUtils.getColor(R.color.textColorPrimary2))
        return textView
    }

    class MyItemDecoration: RecyclerView.ItemDecoration() {
        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
            super.getItemOffsets(outRect, view, parent, state)
            val pos = parent.getChildLayoutPosition(view)
            val linearLayoutManager = parent.layoutManager as LinearLayoutManager
            when (pos) {
                linearLayoutManager.itemCount - 2 -> {
                    outRect.bottom = DensityUtils.dip2px(9f)
                }
                linearLayoutManager.itemCount - 1 -> {
                    outRect.bottom = 0
                }
                else -> {
                    outRect.bottom = DensityUtils.dip2px(1f)
                }
            }
        }
    }
}